package com.bicycle.rentalsystem.service.impl.admin;

import com.bicycle.rentalsystem.dao.admin.BicycleDao;
import com.bicycle.rentalsystem.dao.impl.admin.BicycleDaoImpl;
import com.bicycle.rentalsystem.pojo.admin.Bicycle;
import com.bicycle.rentalsystem.pojo.admin.Page;
import com.bicycle.rentalsystem.service.admin.BicycleService;

import java.util.List;
/**
 * 用户业务层实现类
 *
 * 根据需要更改
 */
public class BicycleServiceImpl implements BicycleService {

    private BicycleDao bicycleDao = new BicycleDaoImpl();

    @Override
    public List<Bicycle> searchBicycles() {
        return bicycleDao.queryBicycles();
    }

    @Override
    public int updateBicycle(String id, String name, String describe) {
        return bicycleDao.updateBicycle(id,name,describe);
    }

    @Override
    public Page<Bicycle> page(Integer pageNo, Integer pageSize) {
        Page<Bicycle> page = new Page<>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示数量
        page.setPageSize(pageSize);
        //总记录数
        Integer pageTotalCount = bicycleDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //当前开始索引位置
        Integer begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Bicycle> items = bicycleDao.queryForPageItems(begin, pageSize);
        //设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public void savaBike(Bicycle bike) {
        bicycleDao.saveBike(bike);
    }

    @Override
    public void deleteBikeById(Integer bid) {
        bicycleDao.deleteBike(bid);
    }

    @Override
    public Bicycle getBikeById(Integer bid) {
        return bicycleDao.getBikeById(bid);
    }

    @Override
    public void updateBike(Bicycle bike) {
        bicycleDao.updateBike(bike);
    }

    @Override
    public int queryStatus() {
        return bicycleDao.queryStatus();
    }
}
